package com.asw.springboot.common;

import com.asw.springboot.utils.EncryptionDecryption;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * Created by hxq on 2017/5/12.
 */
@Component
@Primary
@ConfigurationProperties(
        prefix = "asw.db"
)
public class DataBaseProperties extends DataSourceProperties {

    private String dbUsername;

    private String dbPassword;

    private String dbUrl;

    private static EncryptionDecryption des = EncryptionDecryption.getInstance();

    private DataBaseProperties(){}

    private static DataBaseProperties dataBaseProperties = new DataBaseProperties();

    public static DataBaseProperties getInstance() {
        return dataBaseProperties;
    }

    public String getDbUsername() {
        return dbUsername;
    }

    public void setDbUsername(String dbUsername) throws Exception {
        this.dbUsername = dbUsername;
        super.setUsername(des.decrypt(dbUsername));
    }

    public String getDbPassword() {
        return dbPassword;
    }

    public void setDbPassword(String dbPassword)throws Exception  {
        this.dbPassword = dbPassword;
        super.setPassword(des.decrypt(dbPassword));
    }

    public String getDbUrl() {
        return dbUrl;
    }

    public void setDbUrl(String dbUrl) {
        this.dbUrl = dbUrl;
        super.setUrl(dbUrl);
    }

    @Override
    public String toString() {
        return "DataBaseProperties{" +
                "dbUsername='" + dbUsername + '\'' +
                ", dbPassword='" + dbPassword + '\'' +
                ", dbUrl='" + dbUrl + '\'' +
                '}';
    }
}
